import requests
import time

# 全局变量设置ESP32的IP和端口号
ESP32_IP = "192.168.1.108"  # 请根据实际情况修改
ESP32_PORT = 80

def send_request(endpoint):
    url = f"http://{ESP32_IP}:{ESP32_PORT}/{endpoint}"
    try:
        response = requests.get(url)
        if response.status_code == 200:
            print(f"成功触发 {endpoint}: {response.text}")
        else:
            print(f"触发 {endpoint} 失败: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"请求 {endpoint} 时发生错误: {e}")

def main():
    # 设置速度
    send_request("setSpeed?value=5000")
    # 启动电机
    send_request("start")
    # 等待5秒
    time.sleep(5)
    
    send_request("setSpeed?value=-5000")
    send_request("start")
    time.sleep(5)
    # 停止电机
    send_request("stop")
    # 切换灯状态
    send_request("toggleLight")

if __name__ == "__main__":
    main()
